<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="../js/vue.js"></script>
    <title>天气案例_监视简写</title>
</head>
<body>
    <div id="root">
        <h2>今天天气很{{isHot}}</h2>
        <button @click="changeWeather">切换天气</button>
    </div>
    <script type="text/javascript">
        const vm = new Vue({
            el:"#root",
            data(){
                return {
                    state: true,
                }
            },
            methods:{
                changeWeather(){
                    this.state = !this.state
                }
            },
            computed:{
                isHot(){
                    return this.state?"炎热":"凉爽"
                }
            },
            // watch:{
            //     //完整写法
            //     state:{
            //         //初始化时让handler调用一下
            //         immediate: true,
            //         deep: true,
            //         handler(newValue, oldValue){
            //             console.log("watch被调用:新",newValue,"    旧",oldValue)
            //         }
            //     },
            //     //简写
            //     state(newValue, oldValue){
            //         console.log("watch被调用（简写）:新",newValue,"    旧",oldValue)
            //     },  
            // }
        })
        // //正常写法
        // vm.$watch('state',{
        //             //初始化时让handler调用一下
        //             immediate: true,
        //             handler(newValue, oldValue){
        //                 console.log("watch被调用:新",newValue,"    旧",oldValue)
        //             }
        // })
        //简写
        vm.$watch('state',function(newValue, oldValue){
                console.log("watch被调用(简写):新",newValue,"    旧",oldValue)
        })
    </script>
</body>
</html>